﻿<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <title>修改密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css" media="all" />
</head>

<body>
    <div style="width:450px;margin:0px auto;padding-top:100px;">
        <form class="layui-form changePwd" lay-filter="changePwd">
            <input type="hidden" name="id" id="id" lay-filter="id">
            <div class="layui-form-item">
                <label class="layui-form-label">用户账号:</label>
                <div class="layui-input-block">
                    <input id="userAcc" class="layui-input layui-btn-disabled" name="acc" disabled="disabled"
                        readonly="readonly">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">旧密码</label>
                <div class="layui-input-block">
                    <input value="" lay-verType="tips" placeholder="请输入旧密码" name="used" type="password" readonly
                           onfocus="this.removeAttribute('readonly');"
                        lay-verify="sonPwd" class="layui-input pwd" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-block">
                    <input type="password" value="" lay-verType="tips" placeholder="请输入新密码" lay-verify="newPwd" readonly
                           onfocus="this.removeAttribute('readonly');"
                        id="oldPwd" name="oldPwd" class="layui-input pwd" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-block">
                    <input type="password" value="" lay-verType="tips" name="pass" placeholder="请确认密码" readonly
                           onfocus="this.removeAttribute('readonly');"
                        lay-verify="newPwd2" class="layui-input pwd" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" type="button" lay-submit=""
                        lay-filter="changePwd">立即修改</button>
                    <button onclick="resetAs()" type="button" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script src="../../plugins/jquery.min.js"></script>
    <script src="../../js/base.js"></script>
    <script src="../../plugins/layui/layui.js"></script>
    <script>
        function resetAs() {
            layui.layer.closeAll('tips');
            $(':input:not("#userAcc")').val('');
        }

        $(function () {
            $(':input').keydown(function (event) {
                if (event.keyCode === 13) {
                    $('[lay-filter="changePwd"]').click();
                }
            });

            layui.use(['form', 'layer'], function () {

                var form = layui.form;
                $.ajax({
                    url: appendCtx('/ep/sys/users/current-user'),
                    type: "get",
                    success: function (result) {
                        if(result.code == 0){
                            var respData = result.data ;
                            if(respData){
                                $('#userAcc').val(respData.username);
                                $('#id').val(respData.id);
                                $('[name="used"]').val();
                            }
                        }else{
                            layer.msg(result.msg,{
                                zIndex:layer.zIndex
                            });
                        }

                    },
                    error: function (data) {
                        layer.alert(data.responseText, {icon:2});
                    }
                });

                form.verify({
                    sonPwd: function (value) {
                        if (value.length < 1) {
                            return '请填写旧密码';
                        }
                    }
                    , newPwd: function (value) {
                        if (value.length < 1) {
                            return '请填写新密码';
                        } else if (value.length < 5 || value.length > 20) {
                            return '密码应在5位与20位之间';
                        } else if (value === $('[name="used"]').val()) {
                            return "新密码不能和旧密码相同";
                        }
                    }
                    , newPwd2: function (value) {
                        if (value.length < 1) {
                            return '请填写确认密码';
                        } else if (value !== $('#oldPwd').val()) {
                            return '确认密码与新密码不一致';
                        }
                    }
                });


                //监听提交
                form.on('submit(changePwd)', function (data) {
                    layui.layer.closeAll('tips');
                    var dataValue =data.field;
                    $.ajax({
                        url: appendCtx('/ep/sys/users/passwords'),
                        type: "put",
                        contentType: 'application/json; charset=utf-8',
                        data: JSON.stringify({'password' :createMD5(dataValue.pass),'pass':createMD5(dataValue.used),'id':dataValue.id,'userName':dataValue.acc}),
                        success: function (result) {
                            if(result.code == 0){
                                layer.alert('修改成功',{
                                    zIndex:layer.zIndex
                                });
                                $('[name="used"]').val("");
                                $('[name="oldPwd"]').val("");
                                $('[name="pass"]').val("");
                            }else{
                                layer.alert(result.msg,{
                                    zIndex:layer.zIndex
                                });
                            }

                        },
                        error: function (data) {
                            layer.alert(data.responseText, {icon:2});
                        }
                    });
                    return false;
                });

            });//layui.use结束括号
        });//jquery结束括号
    </script>
</body>

</html>